<?php

class OpensslRSA{

    //echo $private_key 私钥;
   public $private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQC2dl48QuPvdJhC9FA6NEOMM+9atX3qgh0ByQcJKRnOQBKFLoTZ
u6hLCPIVtdkPcqJNr8fnrxH+gJvIRmbln3VJPU3jCoQUl5vFLIRFOOH/2GM3R2KE
7vD7aqKUU7HIR83i99fsvZMYKyaZzMNnSRhxx6vGuMoKPExHFFkIwiNBMwIDAQAB
AoGBAJuaoo7dtKk3U8p1tB2PCLP+bnd7I+DkCXzGRZvIU1uTRLgTjfRplwI2pM1j
AbOs3UFHUUICCWRzpWbMs1/L9yaGzs1TmCBv2hFd6Za5ysNjD3bkMfdsevXAs0ZZ
1wMBztGgIb2JMa+l+9i/TAbb88Vyr+N82cnD/imeBi8iEhNRAkEA6UcS9k1I8fWa
qDFCVfVUiV/PXTE+6zk8rESj3H51tOUOYaOF3DGLN1d0lTGX5JriEzRWQsLqPiDf
r+KPv0joTQJBAMg8Lu24/6A24jXbcN/boj1+bV34+P9IOp5DOwn8239TCz7Ea9BW
FiWVFvd6CXjxmsc/1QaGd5cEyJQ8zxqJj38CQQDUCciQnLs7Q5WqX+v+GhsXK0Ff
Yg3g3YvqwhF3sHLpVHL4CRz5gwcZ7KKJoiSApM/tyNrHoEgu5M6uHu+uXWTZAkBO
2qycf7tji9mf3hBgiC0nTG+jj1X3qqkzBETu1P5X8Qs/p7jRreVA3arSwOOUFome
p/urey8DcSXQgvYIfF3fAkEAhht85EnhV2PC23/eu1n82WkYnovwVwT4SAG/64rU
jeKTCUgGwC02+RVSzIqgndUOQS0r13fl6NsEKaLARwRPCQ==
-----END RSA PRIVATE KEY-----';

   //公钥
    public $public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2dl48QuPvdJhC9FA6NEOMM+9a
tX3qgh0ByQcJKRnOQBKFLoTZu6hLCPIVtdkPcqJNr8fnrxH+gJvIRmbln3VJPU3j
CoQUl5vFLIRFOOH/2GM3R2KE7vD7aqKUU7HIR83i99fsvZMYKyaZzMNnSRhxx6vG
uMoKPExHFFkIwiNBMwIDAQAB
-----END PUBLIC KEY-----';

    public $pi_key;
    public $pu_key;

    //判断公钥和私钥是否可用
    public function __construct()
    {
        $this->pi_key =  openssl_pkey_get_private($this->private_key);//这个函数可用来判断私钥是否是可用的，可用返回资源id Resource id
        $this->pu_key = openssl_pkey_get_public($this->public_key);//这个函数可用来判断公钥是否是可用的
     //   print_r($this->pi_key);echo "\n";
     //   print_r($this->pu_key);echo "\n";
    }

    //私钥加密
    /*public function PrivateEncrypt($data){
        openssl_private_encrypt($data,$encrypted,$this->pi_key);
        $encrypted = $this->urlsafe_b64encode($encrypted);//加密后的内容通常含有特殊字符，需要编码转换下，在网络间通过url传输时要注意base64编码是否是url安全的
        return $encrypted;
    }*/

    public function PrivateEncrypt($data){
       // openssl_private_encrypt($data,$encrypted,$this->pi_key);
        $crypto = '';
        foreach (str_split($data, 117) as $chunk) {
            openssl_private_encrypt($chunk, $encryptData, $this->pi_key);
            $crypto .= $encryptData;
        }
        $encrypted = $this->urlsafe_b64encode($crypto);//加密后的内容通常含有特殊字符，需要编码转换下，在网络间通过url传输时要注意base64编码是否是url安全的
        return $encrypted;
    }
    //加密码时把特殊符号替换成URL可以带的内容
    function urlsafe_b64encode($string) {
        $data = base64_encode($string);
        //$data = str_replace(array('+','/','='),array('-','_',''),$data);
        return $data;
    }

    //解密码时把转换后的符号替换特殊符号
    function urlsafe_b64decode($string) {
        $data = str_replace(array('-','_'),array('+','/'),$string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
    }

    //私钥加密的内容通过公钥可用解密出来
    public function PublicDecrypt($encrypted){
       // $encrypted = $this->urlsafe_b64decode($encrypted);
        $crypto = '';
        foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
            openssl_public_decrypt($chunk, $decryptData, $this->pu_key);
            $crypto .= $decryptData;
        }
        //openssl_public_decrypt($encrypted,$decrypted,$this->pu_key);//私钥加密的内容通过公钥可用解密出来
        return $crypto;
    }

    //公钥加密
    public function PublicEncrypt($data){
        //openssl_public_encrypt($data,$encrypted,$this->pu_key);//公钥加密
        $crypto = '';
        foreach (str_split($data, 117) as $chunk) {
            openssl_public_encrypt($chunk, $encryptData, $this->pu_key);
            $crypto .= $encryptData;
        }
        $encrypted = $this->urlsafe_b64encode($crypto);
        return $encrypted;
    }

    //私钥解密
    public function PrivateDecrypt($encrypted)
    {
        $crypto = '';
        foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
            openssl_private_decrypt($chunk, $decryptData, $this->pi_key);
            $crypto .= $decryptData;
        }
        //$encrypted = $this->urlsafe_b64decode($encrypted);
        //openssl_private_decrypt($encrypted,$decrypted,$this->pi_key);
        return $crypto;
    }
}